//
//  BQViewController.m
//  BeeQuick
//
//  Created by Vivitickey on 2017/4/26.
//  Copyright © 2017年 test. All rights reserved.
//

#import "BQViewController.h"
#import "BQButton.h"
#import "BQSearchViewController.h"
#import "BQRightModel.h"
#import "BQShopCartViewController.h"
#import "UIColor+HMAddition.h"
@interface BQViewController () <CAAnimationDelegate>


@property (nonatomic,strong) UIBezierPath *path;



 
@end

@implementation BQViewController{
    
    CALayer *_layer;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self getShopingCount];
    
    
}

#pragma mark - 购物车数据

-(void)setupUI{
    
    [self registerNotification];
    
    self.view.backgroundColor = [UIColor hm_colorWithR:236 G:236 B:236];
    
    UIButton *SearchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    
    [SearchButton setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    
    [SearchButton addTarget:self action:@selector(clickSearchButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:SearchButton];
    
    BQButton *adressButton = [[BQButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
    
    adressButton.text = @"武汉";
    
    [adressButton sizeToFit];
    
    self.navigationItem.titleView = adressButton;

}

-(void)viewWillAppear:(BOOL)animated{
    
    [self getShopingCount];
    
}

#pragma mark - 注册通知
- (void)registerNotification {
    
    // 增加的
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReciveIncrementNotification:) name:kIncrementNotification object:nil];
    
    // 减少的
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReciveDecrementNotification:) name:kDecrementNotification object:nil];
    
    
    //购物车通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didClickShopingCarButton) name:kShopingCarNotification object:nil];
}

#pragma mark - 购物车通知

-(void)didClickShopingCarButton{
    
    [self getShopingCount];
    
}

#pragma mark - 减少的通知
- (void)didReciveDecrementNotification:(NSNotification *)noti {
    
    [self getShopingCount];
}

#pragma mark - 增加的通知
- (void)didReciveIncrementNotification:(NSNotification *)noti {
    
    // 获取传递过来的model
    BQRightModel *rightModel = noti.object;
    
    // 获取传递的位置 , 起始点
    CGPoint startPoint = [noti.userInfo[@"point"] CGPointValue];
    
    // 通过起始点计算 控制点
    CGPoint controlPoint = CGPointMake(_ScreenWidth / 2, startPoint.y - 80);
    
    // 终点
    CGPoint endPoint = CGPointMake(_ScreenWidth / 2 + 60, _ScreenHeight - 22);
    
    // 实例化imageView
    UIImageView *iconImage = [[UIImageView alloc] init];
    
    [iconImage sd_setImageWithURL:[NSURL URLWithString:rightModel.img]];
    
    _layer = [CALayer layer];
    _layer.contents = (id)iconImage.layer.contents;
    
    _layer.contentsGravity = kCAGravityResizeAspectFill;
    _layer.bounds = CGRectMake(0, 0, 100, 100);
    [_layer setCornerRadius:CGRectGetHeight([_layer bounds]) / 2];
    _layer.masksToBounds = YES;
    // 原View中心点
    _layer.position = [noti.userInfo[@"point"] CGPointValue];
    [self.view.layer addSublayer:_layer];
    self.path = [UIBezierPath bezierPath];
    // 起点
    [_path moveToPoint:_layer.position];
    [_path addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = _path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    expandAnimation.duration = 0.5f;
    expandAnimation.fromValue = [NSNumber numberWithFloat:1];
    expandAnimation.toValue = [NSNumber numberWithFloat:2.0f];
    expandAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    narrowAnimation.beginTime = 0.5;
    narrowAnimation.fromValue = [NSNumber numberWithFloat:2.0f];
    narrowAnimation.duration = 1.5f;
    narrowAnimation.toValue = [NSNumber numberWithFloat:0.3f];
    
    narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,expandAnimation,narrowAnimation];
    groups.duration = 2.0f;
    groups.removedOnCompletion=NO;
    groups.fillMode=kCAFillModeForwards;
    groups.delegate = self;
    
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeBoth;
    
    [_layer addAnimation:groups forKey:@"group"];
    
    
}



#pragma mark - 动画结束之后
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    if (anim == [_layer animationForKey:@"group"]) {
        
        [_layer removeFromSuperlayer];
        _layer = nil;
    }
    
    
    [self getShopingCount];
    
}


#pragma mark - target方法 -

-(void)clickSearchButton{
    
    
    [self.navigationController pushViewController:[BQSearchViewController new] animated:YES];
    
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)getShopingCount{
    
    FMDatabase *db1 = [[FMDatabase alloc] initWithPath:localDBPath];
    [db1 open];

        
        NSInteger shopingCount = 0;
        
        FMDatabase *db = [[FMDatabase alloc] initWithPath:localDBPath];
        [db open];

    
        if ([db tableExists:@"BeeQuick"]) {
        
        FMResultSet *set = [db executeQuery:@"select count from BeeQuick where count > 0"];
        
        while ([set next]) {
            NSInteger count = [set intForColumn:@"count"];
            
            shopingCount += count;
        }
        
        [db close];
        UITabBarItem * item=[self.tabBarController.tabBar.items objectAtIndex:2];
        
        //    item.badgeColor = [UIColor redColor];
        
        if (shopingCount == 0) {
            
            item.badgeValue = nil;
            
        }else{
            
            item.badgeValue = [NSString stringWithFormat:@"%zd",shopingCount];
            
        }
        }else{
            return;
        }

    [db1 close];

}


@end
